/*
** StockDB
**
** Copyright (C) 2012 - ? doublegsoft
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __STORAGE_H__
#define __STORAGE_H__

#include <stdio.h>

#include "stockdb.h"

#define DATAFILE_SUFFIX ".sdb"

/*!
** Appends a stock_t data into storage file.
**
** @param[in] dir
**              storage file directory
**
** @param[in] name
**              storage file name
**
** @param[in] stock
**              stock information
*/
void
stockdb_append_stock(const char *dir, const char *name, stock_t *stock);

/*!
** Appends a stock_daily_t data into storage file.
**
** @param[in] dir
**              storage file directory
**
** @param[in] name
**              storage file name
**
** @param[in] code
**              stock code
**
** @param[in] data
**              stock daily data
**
** @return error code
*/
int
stockdb_append_stock_daily(const char *dir, const char *name, const char *code, stock_daily_t *data);

/*!
** Changes the given stock in storage file.
**
** @param[in] stock
**              stock information
*/
void
stockdb_change_stock(FILE *dbf, stock_t *stock);

/*!
** Creates a stock database with the given name.
**
** @param[in] dir
**              database directory
**
** @param[in] name
**              database name
*/
void
stockdb_create_database(const char *dir, const char *name);

/*!
** Opens a stock datafile with the given name.
**
** @param[in] dir
**              data file directory
**
** @param[in] name
**              data file name
**
** @param[in] mode
**              file open mode
**
** @return database file
*/
static FILE *
stockdb_open_datafile(const char *dir, const char *name, const char *mode);

/*!
** Closes the given stockdb file.
**
** @param[in] dbf
**              stockdb file pointer
*/
static void
stockdb_close_datafile(FILE *dbf);

/*!
** Finds a stock_t object with the given code and position offset in the data
** file.
**
** @param[in] dir
**              data file directory
**
** @param[in] name
**              data file name
**
** @param[in] code
**              stock code
**
** @param[in] offset
**              position in the data file
**
** @return stock_t data
*/
static stock_t *
stockdb_find_stock(const char *dir, const char *name, const char *code, ulong *offset);

/*!
** Reads stocks_t data from stockdb data file.
**
** @param[in] dir
**              data file directory
**
** @param[in] name
**              data file name
**
** @param[out] size
**              data file total bytes size
**
** @return stocks_t data
*/
static stocks_t *
stockdb_read_stocks(const char *dir, const char *name, ulong *size);

/*!
** Reads stock_t data from stockdb data file.
**
** @param[in] dir
**              data file directory
**
** @param[in] name
**              data file name
**
** @param[in] offset
**              file position offset
**
** @return stock_t data
*/
static stock_t *
stockdb_read_stock(const char *dir, const char *name, ulong offset);

/*!
** Reads stock_daily_t data from stockdb data file.
**
** @param[in] dir
**              data file directory
**
** @param[in] name
**              data file name
**
** @param[in] offset
**              file position offset
**
** @return stock_t data
*/
static stock_daily_t *
stockdb_read_stock_daily(const char *dir, const char *name, ulong offset);

/*!
** Replaces stock_daily_t data in the position of stockdb data file.
**
** @param[in] dir
**              data file directory
**
** @param[in] name
**              data file name
**
** @param[in] offset
**              file position offset
**
** @param[in] data
**              data to be written
**
** @param[in] size of the given data
*/
static void
stockdb_replace_datafile(const char *dir, const char *name, ulong offset, void *data, size_t size);

/*!
** Appends stock_daily_t data to the end of stockdb data file.
**
** @param[in] dir
**              data file directory
**
** @param[in] name
**              data file name
**
** @param[in] data
**              data to be written
**
** @param[in] size of the given data
*/
static void
stockdb_append_datafile(const char *dir, const char *name, void *data, size_t size);

/*!
** Gets the size of the data file.
**
** @param[in] dir
**              data file directory
**
** @param[in] name
**              data file name
**
** @return size of the data file
*/
static ulong
stockdb_sizeof_datafile(const char *dir, const char *name);


#endif /* __STORAGE_H__ */
